package com.bydq.patrolsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bydq.patrolsystem.entity.PsUser;
import com.bydq.patrolsystem.mapper.PsUserMapper;
import com.bydq.patrolsystem.service.PsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 写代码是不可能写代码的啦~~
 * @since 2020-05-22
 */
@Service
public class PsUserServiceImpl extends ServiceImpl<PsUserMapper, PsUser> implements PsUserService {

    @Autowired
    private PsUserMapper psUserMapper;

    @Override
    public boolean checkLogin(String username, String password, Model model) {
        //登录验证

        if(username==null||password==null){
            model.addAttribute("msg","用户名或密码不能为空");
        }

        if(username.length()<6||username.length()>20){
            model.addAttribute("msg","用户名是必须6到20位字符");
            return false;
        }

        if(password.length()<6||password.length()>20){
            model.addAttribute("msg","密码必须6到20位字符");
            return false;
        }

        //检查用户是否被禁用
        if(psUserMapper.selectStatusByUserCode(username)==0){
            model.addAttribute("msg","用户被禁用");
            return false;
        }



        model.addAttribute("msg","用户名或密码错误");

        //根据用户名查询对应的密码
        String pwd = psUserMapper.selectPasswordByUsername(username);

        return Objects.equals(pwd,password);
    }

    @Override
    public PsUser getUserByCode(String username) {
            //这里所指的用户名实际上是用户编码
        return psUserMapper.selectOne(new QueryWrapper<PsUser>().eq("userCode",username));
    }

    @Override
    public String ordinaryCheckAndUpdate(PsUser user, String password, String repassword, HttpSession session) {
        if(user==null||repassword==null||password==null)
            return "请按规范提交表单";

        if(!Objects.equals(password,repassword))
            return "两次密码输入不一致";

        if(password.length()<6||password.length()>20)
            return "密码必须是6到20位字符";

        String realPaddword = psUserMapper.selectPasswordByUsername(user.getUserCode());

        //验证旧的密码是否正确
        if(!Objects.equals(realPaddword,user.getUserPassword()))
            return "密码输入错误无法通过验证";

        //验证旧的密码是否是这个
        if(Objects.equals(realPaddword,password))
            return "新的密码不能和上一次相同";

        //开始修改 只修改能修改的字段  密码 性别 年龄 联系电话 电子邮箱
        user.setUserPassword(password);
        psUserMapper.ordinaryUpdateByUserCode(user);

        //更新当前的用户信息
        session.setAttribute("user",psUserMapper.selectOne(new QueryWrapper<PsUser>().eq("userCode",user.getUserCode())));

        return "success";
    }

    @Override
    public IPage<PsUser> selectPageByEditions(Integer currentPage, Integer pageSize, String userName, String userStatus) {
        Page<PsUser> page = new Page<>(currentPage, pageSize);
        QueryWrapper<PsUser> psUserQueryWrapper = new QueryWrapper<PsUser>().like(userName != null, "userName", userName)
                .eq(userStatus != null && !"".equals(userStatus.trim()), "userStatus", userStatus).
                        orderByDesc("creationTime");

        return psUserMapper.selectPage(page,psUserQueryWrapper);
    }

    @Override
    public boolean userCodeExist(String userCode) {

        return psUserMapper.selectCount(new QueryWrapper<PsUser>().eq("userCode",userCode))>0;
    }

    @Override
    public boolean setUserStatus(Integer id, Integer start) {



        return psUserMapper.updateStatus(id,start);
    }
}
